/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hibernatedaos;

import hibentities.Producer;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

/**
 *
 * @author piotrek
 */
@Repository
public class ProducerHibernateDao {

	private SessionFactory sessionFactory;
	private HibernateTemplate hibernateTemplate;

	public ProducerHibernateDao(){

	}
	
	@Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

	/**
	 * Pobieranie obiektu pojedynczego producenta
	 * @param Long id - identyfikator producenta
	 * @return
	 */
	public Producer getProducer( Long id ){

		return this.hibernateTemplate.get(Producer.class, id);

	}

	/**
	 * Pobieranie listy producentów
	 * @return List lista producentów
	 */
	public List<Producer> getProducerList(){

		Session s = this.hibernateTemplate.getSessionFactory().openSession();
		List<Producer> lista = s.createQuery( "from Producer producer" ).setMaxResults(300).list();

		 s.close();
		 return lista;

	}

	/**
	 * Dodawanie nowego producenta przez Hibernate
	 * @param Producer - obiekt producenta
	 * @return boolean
	 */
	public boolean addProducer( Producer producer ){

		this.hibernateTemplate.save( producer );

		return true;

	}

	/**
	 * Aktualizacja pojedynczego rekordu
	 * @param Prducer obiekt producenta
	 * @return boolean
	 */
	public boolean updateProducer( Producer producer ){

		this.hibernateTemplate.update(producer);
		return true;
		
	}

	/**
	 * Kasowanie pojedynczego rekordu
	 * @param Long id identyfikator kasowanego rekordu
	 * @return boolean
	 */
	public boolean deleteProducer( Long id ){

		Producer producer = this.hibernateTemplate.get( Producer.class, id);
		this.hibernateTemplate.delete(producer);
		return true;

	}
}